package com.vhall.component.controller.exam;

import com.vhall.component.controller.exam.facade.ExamApiFacade;
import com.vhall.component.entity.exam.dto.ExamAnswersDTO;
import com.vhall.component.entity.exam.dto.ExamDTO;
import com.vhall.component.entity.exam.vo.*;
import com.vhall.component.entity.page.vo.PageVO;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.ApiSignatureAuth;
import com.vhall.component.plugin.common.annotation.VssTokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.VssToken;
import com.vhall.component.plugin.common.support.VssTokenThreadLocal;
import com.vhall.component.service.exam.ExamConsoleService;
import com.vhall.component.service.exam.ExamService;
import com.vhall.component.service.exam.V2ExamService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @author guoxin.wang
 * @since 2021/7/5
 */
@VhallComponent
@ApiSignatureAuth
public class ExamApiController implements ExamApiFacade {

    @Autowired
    private V2ExamService v2ExamService;

    @Autowired
    private ExamService examService;

    @Autowired
    private ExamConsoleService examConsoleService;

    /**
     * 试卷详情查看
     *
     * @param vo 试卷编号
     * @return
     */
    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public PageVO<ExamDTO> paperList(ExamPaperListReqVo vo) {
        VssToken vssToken = VssTokenThreadLocal.getToken();

        return examService.paperList(vssToken.getThirdPartyUserId(), vo);
    }

    /**
     * 考卷绑定 - 业务逻辑为复制一份, 并绑定
     *
     * @param vo
     * @return
     */
    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public boolean paperBind(ExamVo vo) {
        VssToken vssToken = VssTokenThreadLocal.getToken();
        Token token = new Token();
        token.setAccountId(vssToken.getThirdPartyUserId());
        return examService.paperBind(token, vo);
    }

    /**
     * 试卷 获取列表
     * 和web的console端获取列表是一个逻辑，所以直接调用console端的service
     *
     * @param vo
     * @return
     */
    @ApiSignatureAuth
    @Override
    @VssTokenAuth
    public PageVO<ExamConsoleListRspVO> list(ExamConsoleListReqVo vo) {

        return examConsoleService.list(vo);
    }

    /**
     * 试卷删除记录
     *
     * @param vo
     * @return
     */
    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public Boolean delete(ExamDeleteApiReqVo vo) {
        VssToken token = VssTokenThreadLocal.getToken();
        ExamDeleteReqVo param = new ExamDeleteReqVo();
        param.setExamId(vo.getExamId());
        return examService.delete(token.getThirdPartyUserId(), param);
    }

    /**
     * 批阅列表
     *
     * @param vo
     * @return
     */
    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public PageVO<ExamAnswersDTO> gradeList(ExamGradeListReqVo vo) {
        if (vo.getIsGraded() == null) {
            throw new BusinessException(BizErrorCode.COMP_EXAM_GRAD_NULL);
        }

        ExamAnswerListVo evo = new ExamAnswerListVo();
        BeanUtils.copyProperties(vo, evo);
        return examService.answeredList(evo);
    }

    /**
     * 发布试卷 主播端
     *
     * @param vo
     */
    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public Boolean publish(ExamVo vo) {
        VssToken vssToken = VssTokenThreadLocal.getToken();
        return v2ExamService.publish(vssToken.getThirdPartyUserId(), vo);
    }

    /**
     * 观看端试卷列表
     *
     * @param vo
     */
    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public PageVO<ExamConsoleListRspVO> watchList(ExamWatchListReqVo vo) {
        return examService.watchList(vo);
    }

    /**
     * 是否提交过试卷
     *
     * @param vo
     */
    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public Map<String, Boolean> checkSurvey(ExamVo vo) {
        VssToken vssToken = VssTokenThreadLocal.getToken();
        Token token = new Token();
        token.setAccountId(vssToken.getThirdPartyUserId());
        return v2ExamService.checkSurvey(token, vo);
    }

    /**
     * 观众已作答列表
     *
     * @param vo
     */
    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public PageVO<AnsweredExamResVo> answeredExam(AnsweredExamReqVo vo) {
        vo.setAccountId(VssTokenThreadLocal.getToken().getThirdPartyUserId());
        return v2ExamService.answeredExam(vo);
    }

    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public Boolean gradePush(ExamVo reqVO) {
        if (reqVO.getExamId() == null) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_PARAM);
        }
        VssToken token = VssTokenThreadLocal.getToken();
        Integer accountId = token.getThirdPartyUserId();
        return v2ExamService.gradePush(reqVO, accountId);
    }

    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public Boolean examFinish(ExamVo vo) {
        VssToken token = VssTokenThreadLocal.getToken();
        return v2ExamService.examFinish(token.getThirdPartyUserId(), vo);
    }

    @ApiSignatureAuth
    @VssTokenAuth
    @Override
    public Boolean setGrade(ExamVo vo) {
        VssToken token = VssTokenThreadLocal.getToken();
        return v2ExamService.setGrade(token, vo);
    }
}
